package edu.ynu.se.xiecheng.achitectureclass.demo.entity;

import edu.ynu.se.xiecheng.achitectureclass.common.entity.LogicEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import javax.persistence.*;
import java.util.Collection;
import java.util.List;

@Entity
@Getter
@Setter
@Table(name = "user")
@ApiModel(description = "用户实体")
public class User extends LogicEntity implements UserDetails {

    @Column(name = "username")
    @ApiModelProperty(value = "用户名", required = true)
    private String username;

    @Column(name = "password")
    @ApiModelProperty(value = "密码", required = true)
    private String password;

    @Column(name = "role")
    @ApiModelProperty(value = "用户角色", required = true)
    private String role;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 用户只有一个角色，直接返回
        return List.of(new SimpleGrantedAuthority(role));
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}